/*     */ package com.zimbra.cs.service.mail;
/*     */ 
/*     */ import com.google.common.io.Closeables;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.share.ShareNotification;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.MailConstants;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.LogFactory;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.index.MessageHit;
/*     */ import com.zimbra.cs.index.SortBy;
/*     */ import com.zimbra.cs.index.ZimbraHit;
/*     */ import com.zimbra.cs.index.ZimbraQueryResults;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxIndex;
/*     */ import com.zimbra.cs.mailbox.Message;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.mime.MPartInfo;
/*     */ import com.zimbra.cs.mime.Mime;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.io.IOException;
/*     */ import java.util.EnumSet;
/*     */ import java.util.HashSet;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ import javax.mail.MessagingException;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class GetShareNotifications
/*     */   extends MailDocumentHandler
/*     */ {
/*  51 */   private static final Log sLog = LogFactory.getLog(GetShareNotifications.class);
/*  52 */   private final String query = "type:xml/x-zimbra-share AND in:inbox";
/*  53 */   private final Set<MailItem.Type> SEARCH_TYPES = EnumSet.of(MailItem.Type.MESSAGE);
/*     */   
/*     */   public Element handle(Element request, Map<String, Object> context) throws ServiceException
/*     */   {
/*  57 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  58 */     Mailbox mbox = getRequestedMailbox(zsc);
/*  59 */     OperationContext octxt = getOperationContext(zsc, context);
/*  60 */     Element response = zsc.createElement(MailConstants.GET_SHARE_NOTIFICATIONS_RESPONSE);
/*  61 */     HashSet<String> shares = new HashSet();
/*     */     
/*  63 */     ZimbraQueryResults zqr = null;
/*     */     try {
/*  65 */       zqr = mbox.index.search(octxt, "type:xml/x-zimbra-share AND in:inbox", this.SEARCH_TYPES, SortBy.DATE_DESC, 10);
/*  66 */       while (zqr.hasNext()) {
/*  67 */         ZimbraHit hit = zqr.getNext();
/*  68 */         if ((hit instanceof MessageHit)) {
/*  69 */           Message message = ((MessageHit)hit).getMessage();
/*     */           try {
/*  71 */             for (MPartInfo part : Mime.getParts(message.getMimeMessage())) {
/*  72 */               String ctype = StringUtil.stripControlCharacters(part.getContentType());
/*  73 */               if ("xml/x-zimbra-share".equals(ctype)) {
/*  74 */                 ShareNotification sn = ShareNotification.fromMimePart(part.getMimePart());
/*  75 */                 String shareItemId = sn.getGrantorId() + ":" + sn.getItemId();
/*  76 */                 if (shares.contains(shareItemId))
/*     */                 {
/*     */ 
/*     */ 
/*  80 */                   sLog.info("deleting stale notification %s", new Object[] { Integer.valueOf(message.getId()) });
/*  81 */                   mbox.delete(octxt, message.getId(), MailItem.Type.MESSAGE);
/*     */                 }
/*     */                 else {
/*  84 */                   shares.add(shareItemId);
/*     */                   
/*  86 */                   Element share = response.addElement((sn.isRevoke()) || (sn.isExpire()) ? "revoke" : "share");
/*  87 */                   if (sn.isExpire()) {
/*  88 */                     share.addAttribute("expire", true);
/*     */                   }
/*  90 */                   Element g = share.addUniqueElement("grantor");
/*  91 */                   g.addAttribute("id", sn.getGrantorId());
/*  92 */                   g.addAttribute("email", sn.getGrantorEmail());
/*  93 */                   g.addAttribute("name", sn.getGrantorName());
/*  94 */                   Element l = share.addUniqueElement("link");
/*  95 */                   l.addAttribute("id", sn.getItemId());
/*  96 */                   l.addAttribute("name", sn.getItemName());
/*  97 */                   l.addAttribute("view", sn.getView());
/*  98 */                   l.addAttribute("perm", sn.getPermissions());
/*  99 */                   String status = message.isUnread() ? "new" : "seen";
/* 100 */                   share.addAttribute("status", status);
/* 101 */                   share.addAttribute("id", "" + message.getId());
/* 102 */                   share.addAttribute("d", message.getDate());
/* 103 */                   if ((sn.isRevoke()) || (sn.isExpire()))
/*     */                   {
/* 105 */                     mbox.delete(octxt, message.getId(), MailItem.Type.MESSAGE); }
/*     */                 }
/*     */               }
/*     */             }
/*     */           } catch (IOException e) {
/* 110 */             ZimbraLog.misc.warn("can't parse share notification", e);
/*     */           } catch (MessagingException e) {
/* 112 */             ZimbraLog.misc.warn("can't parse share notification", e);
/*     */           }
/*     */         }
/*     */       }
/*     */     } finally {
/* 117 */       Closeables.closeQuietly(zqr);
/*     */     }
/*     */     
/* 120 */     return response;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/mail/GetShareNotifications.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */